﻿
/*
 * 文件名：User.cs
 * 类名：XHCTMS.DAL.User
 * 创建人：杨学鹏
 * 创建时间：2013-5-21
 * 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace XHCTMS.DAL
{
    public class ClassroomPerformance
    {
        /// <summary>
        /// 查询班级信息
        /// </summary>
        /// <param name="ClassId">所要查找的班级Id</param>
        /// <returns>返回DataTable类型 null/异常</returns>
        public DataTable GetClassStudent(int ClassId)
        {
            string SqlStatement = "select * from student where student.student_class_Id=" + ClassId + "";
            return new DBHELP.Common().GetDataTable(SqlStatement);
        }

        /// <summary>
        /// 给学生设置分数
        /// </summary>
        /// <param name="StudentScores">学生分数</param>
        /// <returns>返回int类型 >=1/操作成功, 0/失败, -1/异常</returns>
        public int SetStudentScores(string[] StudentScores)
        {
            string SqlStatement = GetStudentScoresSqlStatement(StudentScores);
            return new DBHELP.Common().ExcuteQuery(SqlStatement);
        }


        /// <summary>
        /// 跟据学号得到对应的分数 并拼接SQL语句
        /// </summary>
        /// <param name="StudentScores">学生得分信息</param>
        /// <returns>返回SQL语句</returns>
        string GetStudentScoresSqlStatement(string[] StudentScores)
        {
            StringBuilder SqlStatement = new StringBuilder();
            for (int i = 0; i < StudentScores.Count(); i += 2)
            {
                SqlStatement.Append("insert into performance select teaching.teaching_id," + GetScores(StudentScores[i + 1]) + ",");
                SqlStatement.Append("'" + StudentScores[i + 1] + "',student_class_Id,");
                SqlStatement.Append(@"student.student_id,CONVERT(varchar(20),getdate(),20) from student,teaching,class
                where class.class_id=student.student_class_Id and  teaching.teaching_class_Id=class.class_id");
                SqlStatement.Append(" and student.xhstudent_id='" + StudentScores[i] + "'");
            }
            return SqlStatement.ToString();
        }

        /// <summary>
        /// 跟据原因得到分数
        /// </summary>
        /// <param name="reason">原因</param>
        /// <returns>返回对应的分数</returns>
        string GetScores(string state)
        {
            string result = string.Empty;

            switch (state)
            {
                case "优秀":
                    result = "2";
                    break;
                case "良好":
                    result = "1";
                    break;
                case "三无":
                    result = "-1";
                    break;
                case "顶撞老师":
                    result = "-5";
                    break;
                case "玩手机":
                    result = "-2";
                    break;
                case "玩游戏":
                    result = "-3";
                    break;
                default: result = "0";
                    break;
            }
            return result;
        }
    }
}
